package com.venus.finance.fee.service.impl;

import java.util.List;

import com.venus.common.service.impl.BaseServiceImpl;
import com.venus.common.utils.ConditionUtil;
import com.venus.common.utils.MathUtil;
import com.venus.component.page.PageInfo;
import com.venus.finance.fee.dao.IFeeDAO;
import com.venus.finance.fee.service.IFeeService;
import com.venus.model.AccountVO;
import com.venus.model.FeeVO;

public class FeeServiceImpl extends BaseServiceImpl implements IFeeService {

	private IFeeDAO feeDAO;
	
	public void setFeeDAO(IFeeDAO feeDAO){this.feeDAO = feeDAO;}

	@Override
	public void deleteFeeVO(String[] strFeeIds) {
		for(String strFeeId : strFeeIds){
			FeeVO objFeeVO = this.feeDAO.getModel(FeeVO.class, Integer.parseInt(strFeeId));
			// 删除之后将账户信息金额恢复
			AccountVO objAccountVO = objFeeVO.getAccount(); //this.feeDAO.getModel(AccountVO.class, objFeeVO.getAccount().getAccountId());
			// 判断支出,收入
			if(objFeeVO.getFeeType().getFeeType() == 0){ // 支出
				// 账户金额+消费金额
				objAccountVO.setAmount(MathUtil.round(objAccountVO.getAmount() + objFeeVO.getAmount(), 2).floatValue());
			}else{ // 收入
				// 账户金额-收入金额
				objAccountVO.setAmount(MathUtil.round(objAccountVO.getAmount() - objFeeVO.getAmount(), 2).floatValue());
			}
			this.feeDAO.deleteModel(objFeeVO);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FeeVO> findAllFeeVOs(List<ConditionUtil> lstConditionUtils) {
		return this.feeDAO.exeHql("from FeeVO where 1=1",lstConditionUtils);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> groupFeeVOByFeeTypeId(List<ConditionUtil> lstConditionUtils) {
		return this.feeDAO.exeHql("select FeeTypeVO.feeTypeId,FeeTypeVO.feeTypeName,sum(FeeVO.amount) from FeeVO as FeeVO,FeeTypeVO as FeeTypeVO where 1=1",lstConditionUtils);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> groupFeeVOBySubFeeTypeId(List<ConditionUtil> lstConditionUtils) {
		return this.feeDAO.exeHql("select FeeVO.feeSubType.feeTypeId,FeeTypeVO.feeTypeName,sum(FeeVO.amount) from FeeVO as FeeVO,FeeTypeVO as FeeTypeVO where 1=1",lstConditionUtils);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> groupFeeVOByFeeDate(List<ConditionUtil> lstConditionUtils) {
		return this.feeDAO.exeHql("select FeeVO.feeDate,sum(FeeVO.amount) from FeeVO as FeeVO where 1=1",lstConditionUtils);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> groupLastYearMonthByFeeDate(List<ConditionUtil> lstConditionUtils) {
		return this.feeDAO.exeHql("select DATE_FORMAT(FeeVO.feeDate,'%Y-%m'),FeeVO.feeType.feeTypeId,SUM(FeeVO.amount) from FeeVO as FeeVO where 1=1",lstConditionUtils);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> groupLastYearMonthByFeeType(List<ConditionUtil> lstConditionUtils) {
		return this.feeDAO.exeHql("select DATE_FORMAT(FeeVO.feeDate,'%Y-%m'),FeeVO.feeType.feeType,SUM(FeeVO.amount) from FeeVO as FeeVO where 1=1",lstConditionUtils);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> groupFeeVOByFeeType(List<ConditionUtil> lstConditionUtils) {
		return this.feeDAO.exeHql("select FeeVO.feeType.feeType,SUM(FeeVO.amount) from FeeVO as FeeVO where 1=1",lstConditionUtils);
	}
	
	@Override
	public FeeVO getFeeVO(Integer iFeeId) {
		return this.feeDAO.getModel(FeeVO.class, iFeeId);
	}

	@Override
	public void saveOrUpdateFeeVO(FeeVO objFeeVO) {
		this.feeDAO.saveOrUpdateModel(objFeeVO);
	}

	@Override
	public PageInfo getPageInfo(int iPageRecord, int iCurPage, List<ConditionUtil> lstConditionUtils) {
		String strCountHql = "select count(FeeVO.feeId) from FeeVO as FeeVO where 1=1";
		String strRecordHql = "from FeeVO where 1=1";
		return this.feeDAO.getPageInfo(strCountHql, strRecordHql, lstConditionUtils, iPageRecord, iCurPage);
	}


}
